package csc.daonjpa.java.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.BaseInfo;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.Transactions;
import csc.daonjpa.java.service.AccountService;
import csc.daonjpa.java.service.BaseInfoService;
import csc.daonjpa.java.service.CustomerService;
import csc.daonjpa.java.service.TargetService;
import csc.daonjpa.java.service.TransactionService;

@Controller
public class TransferController {
	private static final Logger LOG = Logger.getLogger(AccountController.class);

	private AccountService accountService;
	private CustomerService customerService;
	private TargetService targetService;
	private BaseInfoService baseInfoService;
	private TransactionService transactionService;
	
	public TransactionService getTransactionService() {
		return transactionService;
	}
	@Autowired
	public void setTransactionService(TransactionService transactionService) {
		this.transactionService = transactionService;
	}
	public BaseInfoService getBaseInfoService() {
		return baseInfoService;
	}
	@Autowired
	public void setBaseInfoService(BaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}
	public TargetService getTargetService() {
		return targetService;
	}
	@Autowired
	public void setTargetService(TargetService targetService) {
		this.targetService = targetService;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	@Autowired
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	

	public AccountService getAccountService() {
		return accountService;
	}
	@Autowired
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
	/**
	 * transfer money to targetAccount
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/transferMoney")
	public String transferMoney(HttpSession session, Model model) {
		String loginId = session.getAttribute("loginSession").toString();
		List<TargetAccount> lstTarget = targetService.getTargetAccounts();
		List<Account> lstAccount = accountService.getAccountByCustomer(loginId);
		model.addAttribute("lstTargets", lstTarget);
		model.addAttribute("lstAccount",lstAccount);
		return "transferMoney";
	}	
	/**
	 * create transaction and save it to database
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/createTransaction",method = RequestMethod.POST)
	public String transferMoney(HttpServletRequest request, Model model) {
		
		Long id_source = Long.parseLong(request.getParameter("sourceAccount"));
		String accNum_target = request.getParameter("targetAccount");
		Double amount  = Double.parseDouble(request.getParameter("amount"));
		
		Account account = accountService.getAccount(id_source);
		TargetAccount targetAccount = targetService.getTargetAccount(accNum_target);
		Transactions transaction = new Transactions(account,targetAccount, amount, new Date());
		BaseInfo baseInfo = baseInfoService.getBaseInfo(id_source);
		
		if (baseInfo.getAvailableAmount() > amount) {
			Double newMoney = baseInfo.getAvailableAmount() - amount;
			transactionService.addTransaction(transaction);
			baseInfoService.updateBaseInfo(newMoney, id_source);
			model.addAttribute("successMessage", "Transfered money successfully");
		} else {
			model.addAttribute("errorMessage", "The current amount is not sufficient for the transaction");
		}
		return "forward:/transferMoney";
	}
	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(Exception ex) {
		ModelAndView model = new ModelAndView("transferMoney");
		model.addObject("errorMessage", "Exception occurred");
		return model;
	}
}
